package net.jkdmanager.server.services.applicationstatuses;

import java.io.Serializable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.stereotype.Service;

import net.jkdmanager.common.api.messages.ApplicationStatusesMessage;
import net.jkdmanager.common.api.plugins.RunnerI;
import net.jkdmanager.common.services.messaging.MessageListener;

@Service
public class ApplicationStatusesServiceImpl implements ApplicationStatusesService, MessageListener {

	private Map<String, Map<Long, RunnerI.Status>> statuses = new ConcurrentHashMap<String, Map<Long, RunnerI.Status>>();
		
	public void agentDisconnected(String agentId) {
		//removing application statuses as they might not be valid since agent is not up!
		statuses.remove(agentId);
	}
	
	public void receiveMessage(Serializable message) {
		if (message instanceof ApplicationStatusesMessage) {
			ApplicationStatusesMessage apsMessage = (ApplicationStatusesMessage)message;
			statuses.put(apsMessage.getAgentId(), apsMessage.getAppStatuses());
		}
	}
	
	public Map<String, Map<Long, RunnerI.Status>> getStatuses() {
		return statuses;
	}

}
